#region Directives
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;

using VisualStudioSensor;
using TeamFoundationServerSensor;
#endregion

/// <summary>
/// Web Service Endpoint for the <seealso href="BuildStatusChangeEvent"/>
/// </summary>
/// <remarks>
/// To Register this Notification Endpoint you will need to use the <seealso href="http://blogs.msdn.com/psheill/archive/2006/02/01/522386.aspx">BisSubscribe.exe</seealso> tool. 
/// For more information about Team Foundation Server Notifications <seealso href="http://blogs.msdn.com/buckh/archive/2006/04/10/event_filtering.aspx"/>
/// </remarks>
[WebService(Namespace = "http://schemas.conchango.com/TeamFoundation/2005/06/Services/Notification/Extensibility", Description = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class BuildStatusChangeEndpoint : TeamFoundationServerSensor.EndpointBase {

    /// <summary>
    /// Endpoint required to accept incoming <see href="BuildStatusChangeEvent"/> notifications
    /// </summary>
    /// <param name="eventXml">Serialized version of the <see href="BuildStatusChangeEvent"/></param>
    /// <param name="tfsIdentityXml">Serialized version of <see href="TFSIdentity"/></param>
    /// <returns>True if method succeeded in reading data. False otherwise.</returns>
    [WebMethod]
    [SoapDocumentMethod("http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03/Notify", RequestNamespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03")]
    public override Boolean Notify(string eventXml, string tfsIdentityXml) {
        BuildStatusChangeEvent buildStatusChangeEvent = this.CreateInstance<BuildStatusChangeEvent>(eventXml);
        TFSIdentity tfsIdentity = this.CreateInstance<TFSIdentity>(tfsIdentityXml);

        Boolean success = false;

        try {

            // Common data sent by Hackystat sensors
            Dictionary<string, string> pMap_DevEvent = new Dictionary<string, string>();
            pMap_DevEvent.Add("Author", buildStatusChangeEvent.ChangedBy);
            pMap_DevEvent.Add("Module", "Build");

            // Extra useful data to send.
            pMap_DevEvent.Add("Title", buildStatusChangeEvent.Title);
            pMap_DevEvent.Add("Project", buildStatusChangeEvent.TeamProject);
            pMap_DevEvent.Add("ChangedBy", buildStatusChangeEvent.ChangedBy);
            pMap_DevEvent.Add("ChangeTime", buildStatusChangeEvent.ChangedTime);
            pMap_DevEvent.Add("OldValue", buildStatusChangeEvent.StatusChange.OldValue);
            pMap_DevEvent.Add("CurrentValue", buildStatusChangeEvent.StatusChange.NewValue);
            pMap_DevEvent.Add("Subscriber", buildStatusChangeEvent.Subscriber);
            pMap_DevEvent.Add("TimeZoneOffset", buildStatusChangeEvent.TimeZoneOffset);
            pMap_DevEvent.Add("URL", buildStatusChangeEvent.Url);

            // Attempt to send the build result.
            SensorShellProxy.TheInstance.SendReviewActivityData("Team Foundation Server",
                buildStatusChangeEvent.Url,
                buildStatusChangeEvent.Id,
                buildStatusChangeEvent.StatusChange.NewValue,
                buildStatusChangeEvent.TeamProject,
                pMap_DevEvent);
            success = true;
        }
        catch (Exception e) {
            Logger.TheInstance.Log("Error sending build status change data. Data will be lost. " + e, LogLevel.Error);
        }

        return success;
    }
}